package xcmg.device.service.sale;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import xcmg.device.dao.entity.sale.SaleIntentionDeleteReasonDO;
import xcmg.device.dao.mapper.sale.SaleIntentionDeleteReasonMapper;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.sale.SaleIntentionDeleteReasonVO;
import xcmg.device.service.vo.sale.SaleIntentionDetailVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 阿斯顿 on 2018/8/14.
 */
@Service
public class SaleIntentionDeleteReasonService extends BasicController {
    @Autowired
    private SaleIntentionDeleteReasonMapper saleIntentionDeleteReasonMapper;
    @Autowired
    private FastGenClient fastGenClient;

    public ErrorCode inserReason(SaleIntentionDeleteReasonDO saleIntentionDeleteReasonDO){
        return saleIntentionDeleteReasonMapper.insert(saleIntentionDeleteReasonDO)>0?ErrorCode.Success:ErrorCode.Failure;
    }

    /**
     * 添加删除原因记录
     * */
    public ErrorCode deleteReason(SaleIntentionDetailVO saleIntentionDetailVO,String orgId, String userId,String userName){
        ActionResult<String> textResult=fastGenClient.textGuid();
        if(textResult.getCode()!=ErrorCode.Success.getCode()){
            return ErrorCode.IllegalArument;
        }
        if(saleIntentionDetailVO==null||orgId==null){
            return ErrorCode.IllegalArument;
        }
        SaleIntentionDeleteReasonDO saleIntentionDeleteReasonDO=new SaleIntentionDeleteReasonDO();
        saleIntentionDeleteReasonDO.setId(textResult.getValue());
        saleIntentionDeleteReasonDO.setDeleteDate(new Date());
        saleIntentionDeleteReasonDO.setMaterialNo(saleIntentionDetailVO.getMaterialsNo());
        saleIntentionDeleteReasonDO.setMaterialName(saleIntentionDetailVO.getMaterialsDes());
        saleIntentionDeleteReasonDO.setSaleNo(saleIntentionDetailVO.getSaleNo());
        saleIntentionDeleteReasonDO.setDeleteName(userName);
        saleIntentionDeleteReasonDO.setDeleteReason(saleIntentionDetailVO.getDeleteReason());
        saleIntentionDeleteReasonDO.setOrgId(orgId);
        saleIntentionDeleteReasonDO.setDeleteId(userId);
       return inserReason(saleIntentionDeleteReasonDO);

    }

    public PageCommonVO list(SearchCommonVO<SaleIntentionDeleteReasonVO> condition) {
        PageCommonVO<SaleIntentionDeleteReasonVO> pageCommonVO = new PageCommonVO();
        SaleIntentionDeleteReasonVO saleIntentionDeleteReasonVO=condition.getFilters();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize(), "delete_date desc");
        List<SaleIntentionDeleteReasonDO> deleteReasonDOList=saleIntentionDeleteReasonMapper.list(saleIntentionDeleteReasonVO);
        List<SaleIntentionDeleteReasonVO> deleteReasonVOList=new ArrayList<>();
        if(CollectionUtils.isEmpty(deleteReasonDOList)){
            deleteReasonDOList=new ArrayList<>();
        }
        for(SaleIntentionDeleteReasonDO saleIntentionDeleteReasonDO:deleteReasonDOList){
            SaleIntentionDeleteReasonVO sleIntentionDeleteReasonVO=new SaleIntentionDeleteReasonVO();
            BeanUtils.copyProperties(saleIntentionDeleteReasonDO,sleIntentionDeleteReasonVO);
            deleteReasonVOList.add(sleIntentionDeleteReasonVO);
        }
        pageCommonVO.setPageInfo(new PageInfo(deleteReasonDOList));
        pageCommonVO.setPageInfoList(deleteReasonVOList);
        return pageCommonVO;

    }
}
